//
//  UINavigationBar+TP.m
//  Tripvi
//
//  Created by xiaowuxiaowu on 2017/5/5.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "UINavigationBar+TP.h"
#import <objc/runtime.h>
@implementation UINavigationBar (TP)
static char * kBarBackgroundColorKey = "TPBarBackgroundColor";
static char * kBarBackgroundViewKey = "TPBarBackgroundView";

- (void)setTp_barBackgroundColor:(UIColor *)TP_barBackgroundColor {
    objc_setAssociatedObject(self, kBarBackgroundColorKey, TP_barBackgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if (TP_barBackgroundColor)  {
        [self tp_setNavigationBarBackgroundColor:TP_barBackgroundColor];
    }
}

- (UIColor *)tp_barBackgroundColor {
    return objc_getAssociatedObject(self, kBarBackgroundColorKey);
}


- (void)tp_setTintColor:(UIColor *)tintColor {
    self.tintColor = tintColor;
    
    /// TPBackIndicatorButton需要重绘背景图
//    NSArray * leftViews = [self valueForKey:@"leftViews"];
//    if ([leftViews.firstObject isKindOfClass:[TPBackIndicatorButton class]]) {
//        [((TPBackIndicatorButton *) leftViews.firstObject) TP_resetBackIndicatorWithTintColor:tintColor];
//    }
}

- (void)setTp_backgroundView:(UIView *)tp_backgroundView {
    objc_setAssociatedObject(self, kBarBackgroundViewKey, tp_backgroundView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)tp_backgroundView {
    return objc_getAssociatedObject(self, kBarBackgroundViewKey);
}

- (void)tp_setNavigationBarBackgroundColor:(UIColor *)backgroundColor {
    if (nil == self.tp_backgroundView) {
        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        [self setShadowImage:[UIImage new]];
        
        self.tp_backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, self.frame.size.width, self.frame.size.height+20)];
        self.tp_backgroundView.userInteractionEnabled = NO;
        
        UINavigationBar * contentView = self;
        if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
            contentView = [self valueForKey:@"contentView"];
        }
        /// 创建一个高度为64的TP_backgroundView放在navigationBar上
        [contentView insertSubview:self.tp_backgroundView atIndex:0];
       
    }
    self.tp_backgroundView.backgroundColor = backgroundColor;
}

- (void)tp_setNavigationBarSubViewsAlpha:(CGFloat)alpha {
    [[self valueForKey:@"leftViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
        view.alpha = alpha;
    }];
    
    [[self valueForKey:@"rightViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
        view.alpha = alpha;
    }];
    
    UIView * backIndicatorView = [self valueForKey:@"backIndicatorView"];
    
    /// navigationController.viewControllers.count == 1时，返回箭头图标是隐藏的
    UINavigationController * navigationController = [self valueForKey:@"delegate"];
    if (navigationController.viewControllers.count == 1) {
        backIndicatorView.alpha = 0;
    } else {
        backIndicatorView.alpha = alpha;
    }
    
    UIView * titleView = [self valueForKey:@"titleView"];
    titleView.alpha = alpha;
    
    /// 分系统版本
    UINavigationBar * contentView = self;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
        contentView = [self valueForKey:@"contentView"];
    }
    
    /// 自定义titleView的时候，用KVC取出来可能是nil，可以遍历子视图取出来
    [contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:NSClassFromString(@"UINavigationItemView")]) {
            if (titleView == nil) {
                obj.alpha = alpha;
            }
        }
    }];
}

- (void)tp_setNavigationBarVerticalOffsetY:(CGFloat)offsetY {
    UINavigationBar * contentView = self;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
        contentView = [self valueForKey:@"contentView"];
    }
    contentView.transform = CGAffineTransformMakeTranslation(0, offsetY);
    
    offsetY = MIN(offsetY, 0);
    offsetY = MAX(-44, offsetY);
    [self tp_setNavigationBarSubViewsAlpha:(44.0 + offsetY) / 44.0];
}

- (void)tp_resetNavigationBar {
    [self.tp_backgroundView removeFromSuperview];
    self.tp_backgroundView = nil;
    
    [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self setShadowImage:nil];
    
    UINavigationBar * contentView = self;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
        contentView = [self valueForKey:@"contentView"];
    }
    contentView.transform = CGAffineTransformMakeTranslation(0, 0);
    contentView.transform = CGAffineTransformIdentity;
    
    [self tp_setNavigationBarSubViewsAlpha:1.0];
}
@end
